package com.jiuzhe.commercial.ui.my;

import android.content.Intent;

import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.base.BaseActivity;
import com.jiuzhe.commercial.constant.Constants;
import com.jiuzhe.commercial.http.CallBack;
import com.jiuzhe.commercial.http.HttpClient2;
import com.jiuzhe.commercial.utils.SPUtil;
import com.jiuzhe.commercial.utils.UIUtils;
import com.jungly.gridpasswordview.GridPasswordView;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * Created by huanghao on 2018/5/25.
 */

public class AmendPayActivity extends BaseActivity {
    @BindView(R.id.pswView)
    GridPasswordView pswView;
    //String mEndPsw;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_amend_pay);
        setTitle("修改支付密码");
    }

    @Override
    protected void loadData() {
        pswView.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(String psw) {

            }

            @Override
            public void onInputFinish(String psw) {
                checkOldPwd(psw);
            }
        });
    }

    private void checkOldPwd(final String oldPayPwd) {
        String mer_id = SPUtil.getString(this, Constants.MERCHANTID);
        String id = SPUtil.getString(this, Constants.ID);
        String mingwenPwd = oldPayPwd + id.substring(3,10);
        String pwdMD5 = UIUtils.Md5(mingwenPwd);

        HashMap<String, String> map = new HashMap<>();
        String token = SPUtil.getString(this, Constants.TOKEN);
        String autoSignStr = Constants.CHECKPASSWD.substring(Constants.CHECKPASSWD.lastIndexOf("0/")+1)  + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);
        map.put("user_id",mer_id);
        map.put("old_passwd",pwdMD5);
        map.put("token",id);
        map.put("sign",signStr);

        HttpClient2.post(this, Constants.CHECKPASSWD , map, new CallBack<List<String>>() {
            @Override
            public void onSuccess(List<String> stringList) {
                if(stringList.get(0).equals("37")){
                    UIUtils.showToastSafe("密码错误，重新输入");
                    pswView.clearPassword();
                }
                if(stringList.get(0).equals("38")){
                    UIUtils.showToastSafe("支付密码输入正确");
                    Intent intent = new Intent(AmendPayActivity.this,UpdatePayNewPwdActivity.class);
                    intent.putExtra("oldPwd",oldPayPwd);
                    startActivity(intent);
                }
            }
        });
    }
}
